<template>
  <div>
    <el-form :inline="true" :model="activityQuery" :rules="activityRules">
      <el-form-item label="负责人">
        <el-select
          v-model="activityQuery.ownerId"
          size="small"
          style="width: 120px"
          placeholder="请选择负责人"
          @click="loadOwner"
          clearable
        >
          <el-option
            v-for="item in ownerOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="活动名称">
        <el-input v-model="activityQuery.name" size="small" placeholder="请输入活动名称" clearable />
      </el-form-item>
      <el-form-item label="活动时间">
        <el-date-picker
          v-model="activityQuery.activityTime"
          size="small"
          type="datetimerange"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="活动预算" prop="cost">
        <el-input v-model="activityQuery.cost" size="small" placeholder="请输入活动名称" clearable />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="activityQuery.createTime"
          size="small"
          type="datetime"
          placeholder="请选择创建时间"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="onSearch">搜索</el-button>
        <el-button type="primary" size="small" plain @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-button type="primary" size="small" @click="add">录入市场活动</el-button>
    <el-button type="danger" size="small" @click="batchDel">批量删除</el-button>

    <el-table
      :data="activityList"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column property="ownerDo.name" label="负责人" width="100" />
      <el-table-column property="name" label="活动名称" show-overflow-tooltip />
      <el-table-column property="startTime" label="开始时间" show-overflow-tooltip />
      <el-table-column property="endTime" label="结束时间" show-overflow-tooltip />
      <el-table-column property="cost" label="活动预算" show-overflow-tooltip />
      <el-table-column property="createTime" label="创建时间" show-overflow-tooltip />
      <el-table-column label="操作" align="center" width="230">
        <template #default="scope">
          <el-button type="primary" size="small" @click="view(scope.row.id)">详情</el-button>
          <el-button type="success" size="small" @click="edit(scope.row.id)">编辑</el-button>
          <el-button type="danger" size="small" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="pageSize"
      :total="total"
      @prev-click="toPage"
      @next-click="toPage"
      @current-change="toPage"
    />
  </div>
</template>

<script>
import { doGet } from '../http/httpRequest'
export default {
  name: 'ActivityView',
  data() {
    return {
      // 市场活动搜索表单对象
      activityQuery: {},
      // 市场活动列表对象
      activityList: [{
        ownerDo: {}
      }],
      // 分页时每页显示多少条数据
      pageSize: 0,
      // 分页总共查询出多少条数据
      total: 0,
      // 负责人的下拉列表数据
      ownerOptions: [{}],
      // 定义市场活动搜索表单验证规则
      activityRules: {
        cost: [
          { pattern: /^[0-9]+(\.[0-9]{2})?$/, message: '预算必须为整数或两位小数', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getData(1)
  },
  methods: {
    // 录入市场活动
    add() {
      this.$router.push('/dashboard/activity/add')
    },
    // 编辑市场活动
    edit(id) {
      this.$router.push('/dashboard/activity/edit/' + id)
    },
    // 查看详情
    view(id) {
      this.$router.push('/dashboard/activity/' + id)
    },
    onSearch() {
      this.getData(1)
    },
    onReset() {
      // 搜索条件清空
      this.activityQuery = {}
      this.getData(1)
    },
    // 加载负责人
    loadOwner() {
      doGet('/api/owner', {}).then(resp => {
        if (resp.data.code === 200) {
          this.ownerOptions = resp.data.data
        }
      })
    },
    // 查询市场活动列表
    getData(current) {
      // 开始时间
      let startTime = ''
      // 结束时间
      let endTime = ''
      for (let key in this.activityQuery.activityTime) {
        if (key === '0') {
          startTime = this.activityQuery.activityTime[key]
        }
        if (key === '1') {
          endTime = this.activityQuery.activityTime[key]
        }
      }

      doGet('/api/activity', {
        ownerId: this.activityQuery.ownerId,
        name: this.activityQuery.name,
        cost: this.activityQuery.cost,
        startTime: startTime,
        endTime: endTime,
        createTime: this.activityQuery.createTime,
        current: current
      }).then(resp => {
        if (resp.data.code === 200) {
          this.activityList = resp.data.data.list
          this.pageSize = resp.data.data.pageSize
          this.total = resp.data.data.total
        }
      })
    },
    // 分页查询
    toPage(current) {
      this.getData(current)
    }
  }
}
</script>

<style>
  /* form表单上下之间间距 */
  .el-form-item {
    margin-bottom: 5px;
  }
  .el-form {
    margin-top: 3px;
  }
  .el-table {
    margin-top: 3px;
    height: 458px;
  }
  .el-pagination {
    margin-top: 8px;
  }
</style>